//
//  Jxb_Common_Common.m
//  Jxb_Sdk
//
//  Created by Peter Jin on 14/12/5.
//  Copyright (c) 2014年 Peter Jin. All rights reserved.
//

#import "IGCommon.h"

@implementation IGCommon

+ (void)makeEqualHeightViews:(NSArray *)views inView:(UIView *)containerView TBpadding:(CGFloat)TBpadding LRpadding:(CGFloat)LRpadding viewPadding:(CGFloat)viewPadding
{
    UIView *lastView;
    for (UIView *view in views) {
        [containerView addSubview:view];
        if (lastView) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(LRpadding);
                make.right.mas_equalTo(-LRpadding);
                make.top.mas_equalTo(lastView.mas_bottom).mas_offset(viewPadding);
                make.height.mas_equalTo(lastView.mas_height);
            }];
        }
        else
        {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(containerView.mas_top).mas_offset(TBpadding);
                make.left.mas_equalTo(LRpadding);
                make.right.mas_equalTo(-LRpadding);
            }];
        }
        lastView = view;
    }
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(containerView.mas_bottom).mas_offset(-TBpadding);
    }];
}

+ (void)makeEqualWidthViews:(NSArray *)views inView:(UIView *)containerView TBpadding:(CGFloat)TBpadding LRpadding:(CGFloat)LRpadding viewPadding:(CGFloat)viewPadding
{
    UIView *lastView;
    for (UIView *view in views) {
        [containerView addSubview:view];
        if (lastView) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(TBpadding);
                make.bottom.mas_equalTo(-TBpadding);
                make.left.mas_equalTo(lastView.mas_right).mas_offset(viewPadding);
                make.width.mas_equalTo(lastView);
            }];
        }
        else
        {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(containerView).mas_offset(LRpadding);
                make.top.mas_equalTo(TBpadding);
                make.bottom.mas_equalTo(-TBpadding);
            }];
        }
        lastView = view;
    }
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(containerView).mas_offset(-LRpadding);
    }];
}

+ (void)makeEqualHeightWidthViews:(NSArray *)views inView:(UIView *)containerView viewHeight:(CGFloat)viewHeight lineNums:(int)lineNums Tpadding:(CGFloat)Tpadding LRpadding:(CGFloat)LRpadding HPadding:(CGFloat)HPadding VPadding:(CGFloat)VPadding {
    __block UIView *lastView = nil;
    // 循环创建view
    for (int i = 0; i < views.count; i++) {
        UIView *view = views[i];
        [containerView addSubview:view];
//        view.backgroundColor = [UIColor colorWithHue:(arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5 brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:0.2];
        // 添加约束
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            // 给个高度约束
            make.height.mas_equalTo(viewHeight);
            // 1. 判断是否存在上一个view
            if (lastView) {
                // 存在的话宽度与上一个宽度相同
                make.width.equalTo(lastView);
            }
            // 2. 判断是否是第一列
            if (i % lineNums == 0) {
                // 一：是第一列时 添加左侧与父视图左侧约束
                make.left.mas_equalTo(containerView).offset(LRpadding);
            } else {
                // 二： 不是第一列时 添加左侧与上个view左侧约束
                make.left.mas_equalTo(lastView.mas_right).offset(VPadding);
            }
            // 3. 判断是否是最后一列 给最后一列添加与父视图右边约束
            if (i % lineNums == (lineNums - 1)) {
                make.right.mas_equalTo(containerView).offset(-LRpadding);
            }
            
            // 4. 判断是否为第一行
            if (i / lineNums == 0) {
                // 第一行添加顶部约束
                make.top.mas_equalTo(containerView).offset(Tpadding);
            } else {
                // 其余添加顶部约束
                make.top.mas_equalTo(Tpadding + ( i / lineNums )* (viewHeight + HPadding));
            }
            
        }];
        // 每次循环结束 此次的View为下次约束的基准
        lastView = view;
    }
}

@end
